package de.koo.grum.test.integration

import de.koo.grum.domain.Group;
import grails.test.*

class GroupTests extends GrailsUnitTestCase {
    def newIds

    protected void setUp() {
        super.setUp()

        newIds=[];
        def group = new Group(name:"testGroup")
        group.save(flush:true)
        newIds <<= group.id

    }

    protected void tearDown() {
        super.tearDown()

        newIds.each() { newId ->
            def group=Group.get(newId)
            if (group!=null) {
                group.delete(flush:true)
            }
        }

    }

    void testShow() {
        def group = Group.get( newIds[0] )
        assert group != null

        if(!group) {
            assert group.name == "testGroup"
        }
    }

    void testSearch() {
        def searchString="stGr"
        def groups=Group.findAllByNameLike("%${searchString}%")

        assert groups.size() > 0
    }

    void testList() {
        assert Group.list().size() > 0
    }

    void testCreate() {
        def group = new Group(name:"simpsons")
        if(!group.hasErrors() && group.save(flush:true)) {
            assert group.errors.allErrors.size() == 0
            newIds <<= group.id
        }
        else {
            assert group.errors.allErrors.size() > 0
        }

        group = Group.findByName("simpsons")
        assert group != null
        assert group.name == "simpsons"
    }

    void testUpdate() {
        def group = Group.get( newIds[0] )

        if(group) {
            assert group != null

            group.name = "testUpdate"
            if(!group.hasErrors() && group.save(flush:true)) {
                assert group.errors.allErrors.size() == 0
            }
            else {
                assert group.errors.allErrors.size() > 0
            }
        }
        else {
            assert group == null
        }

        group = Group.get( newIds[0] )
        assert group.name == "testUpdate"
    }

    void testDelete() {
        //tested in tearDown
    }
}
